package com.example.kingdee.kingdeeBridge.kingdeeImpl.impl;

import com.example.kingdee.KingdeeCreate.dto.CreateInDto;
import com.example.kingdee.common.StringUtils;
import com.example.kingdee.common.constant.YiTiConstant;
import com.example.kingdee.exception.ProcedureException;
import com.example.kingdee.kingdeeBridge.common.RequestResult;
import com.example.kingdee.kingdeeBridge.dto.BillWriteInDto;
import com.example.kingdee.kingdeeBridge.entity.SrcBillEntry;
import com.example.kingdee.kingdeeBridge.kingdeeImpl.BillImpl;
import com.example.kingdee.mapper.TBxStMapper;
import com.example.kingdee.sqlTool.SqlTool;
import com.example.kingdee.sqlTool.common.DataObject;
import com.example.kingdee.sqlTool.common.QueryObject;
import com.example.kingdee.sqlTool.sqltoolEnum.FormIdEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

/**
 * 生产汇报接口实现类
 */
@Service
public class ProMoRptImpl implements BillImpl {

    @Autowired
    private SqlTool sqlTool;

    @Autowired
    private TBxStMapper tBxStMapper;

    @Override
    public Map<String, String> selectTargetBillEntry(String fid) {
        QueryObject inventorySelect = new QueryObject(FormIdEnum.proMoRpt);
        ArrayList<String> resultKeyArray = new ArrayList<>();
        inventorySelect.addSelect("FEntity_FEntryID");//明细行id
        resultKeyArray.add("entryId");
        inventorySelect.addSelect("FSrcEntryId");//原单分录内码
        resultKeyArray.add("srcEntryId");
        inventorySelect.addCondition("fid", fid);
        ArrayList<HashMap<String, Object>> targetEntryInfo = sqlTool.weaveResult(inventorySelect, resultKeyArray);
        //构建收料通知单明细行id和原单行id对应关系
        Map<String, String> keyValue = new HashMap<>();
        for (HashMap<String, Object> sld : targetEntryInfo) {
            keyValue.put(sld.get("srcEntryId").toString(), sld.get("entryId").toString());
        }
        return keyValue;
    }

    @Override
    public RequestResult updateTargetBillEntry(String fid, BillWriteInDto billWriteInDto, Map<String, String> targetInfo) {
        List<SrcBillEntry> srcBillEntryList = billWriteInDto.getSrcBillEntryList();
        //将源单明细按照行id进行分类
        MultiValueMap<String, SrcBillEntry> classFySrcBillEntry = sqlTool.getClassFy(srcBillEntryList);
        //修改收料数量，仓库
        List<Map<String, Object>> detailEntityList = new ArrayList<>();
        for (String srcEntryId : classFySrcBillEntry.keySet()) {
            List<SrcBillEntry> srcBillEntries = classFySrcBillEntry.get(srcEntryId);
            for (int i = 0; i < srcBillEntries.size(); i++) {
                SrcBillEntry srcBillEntry = srcBillEntries.get(i);
                Map<String,Object> detailEntity = new LinkedHashMap<>();
                if (i == 0){
                    //修改下推的行
                    detailEntity.put("FEntryID", targetInfo.get(srcEntryId));//行id 必填
                }else {

                }
                //合格数量
                detailEntity.put("FQuaQty", srcBillEntry.getQty());
                //不合格数量
                detailEntity.put("FFailQty", srcBillEntry.getReserved1());
                //人员实作工时
                detailEntity.put("FHrWorkTime", srcBillEntry.getReserved2());
                detailEntityList.add(detailEntity);
            }
        }
        //修改
        DataObject order = new DataObject(FormIdEnum.proMoRpt);
        order.setAttribute("FID", fid);
        order.setAttribute("FEntity", detailEntityList);//订单明细
        try {
            Map<String, Object> result = sqlTool.editForDataObject(order);
            if (result.get("code").toString().equals("200")){
                return RequestResult.ofSuccess(result);
            }else {
                return RequestResult.ofError(result.get("msg").toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ProcedureException("无法调用云星空接口");
        }
    }

    @Override
    public void postPosition(String billNo) {

    }

    @Override
    public void auditTargetBill(String billNo) {
        //提交审核
        sqlTool.auditBill(billNo, FormIdEnum.proMoRpt);
    }

    @Override
    public void deleteTargetBill(String fid) {
        sqlTool.deleteBill(fid, FormIdEnum.proMoRpt);
    }

    public void unAuditDataObject(String fid) {
        DataObject unAuditDataObject = new DataObject(FormIdEnum.proMoRpt);
        unAuditDataObject.addIds(fid);
        sqlTool.unAuditBatchData(unAuditDataObject);
    }
}
